package org.geekbang.time.commonmistakes.springpart1.aop_;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class MyController {

    @Autowired
    private MyService myService;

    @GetMapping("/wrong")
    public String wrongWay() {
        try {
            myService.executeWrongWay01();
            return "This should not be returned.";
        } catch (Exception e) {
            log.error("成功捕获到预期中的异常: {}", e.getClass().getName());
            return "Caught expected exception: " + e.getMessage();
        }
    }

    @GetMapping("/wrong02")
    public String wrong02() {
        try {
            myService.executeWrongWay02();
            return "This should not be returned.";
        } catch (Exception e) {
            log.error("成功捕获到预期中的异常: {}", e.getClass().getName());
            return "Caught expected exception: " + e.getMessage();
        }
    }

    @GetMapping("/correct")
    public String correctWay() {
        myService.executeCorrectWay01();
        return "Successfully executed the correct way!";
    }

    @GetMapping("/correct02")
    public String correctWay02() {
        myService.executeCorrectWay02();
        return "Successfully executed the correct way!";
    }
}